/*
 *  CESliderGroup.cpp
 *  LID Library
 *
 *  Created by Alexander Trutchenko on 09/11/11.
 *  Copyright 2011 League of Independent Developers. All rights reserved.
 *
 */

#include "cocos2dx_extentions/controls/CESliderGroup.h"

#include "cocos2dx_extentions/controls/CESlider.h"

using namespace cocos2d;

namespace cocosext {

#define CE_SLIDER_GROUP_SHIFT_Y 15

CESliderGroup* CESliderGroup::node()
{
	CESliderGroup *pRet = new CESliderGroup();
	CCAssert(pRet != NULL, NULL);
	if (pRet->init())
	{
		pRet->autorelease();
        return pRet;
	}
	else
	{
		CC_SAFE_DELETE(pRet)
		return NULL;
	}
}

bool CESliderGroup::init()
{
//    if (!CCNode::init() )
//        return false;

	return true;
}

/////////////////////////////////////////////////////
// public Cocos2d interface
void CESliderGroup::setIsVisible(bool isVisible)
{
	if (isVisible == this->getIsVisible())
		return;

	CCNode::setIsVisible(isVisible);
	if (!isVisible)
	{
		CCObject* child;
		CCARRAY_FOREACH(this->getChildren(), child)
		{
			CESliderInterface* slider = dynamic_cast<CESliderInterface*>(child);
			slider->setIsEnabled(false);
		}
	}
	else if (_isEnabled)
	{
		CCObject* child;
		CCARRAY_FOREACH(this->getChildren(), child)
		{
			CESliderInterface* slider = dynamic_cast<CESliderInterface*>(child);
			slider->setIsEnabled(true);
		}
	}
}

void CESliderGroup::setIsEnabled(bool isEnabled)
{
	if (_isEnabled == isEnabled)
		return;
	_isEnabled = isEnabled;
	if (this->getIsVisible())
	{
		CCObject* child;
		CCARRAY_FOREACH(this->getChildren(), child)
		{
			CESliderInterface* slider = dynamic_cast<CESliderInterface*>(child);
			slider->setIsEnabled(isEnabled);
		}
	}
}

/////////////////////////////////////////////////////
// Main interface

void CESliderGroup::addChild(CCNode * child, int zOrder, int tag)
{
	CCAssert(dynamic_cast<CESliderInterface*>(child) != NULL, "CESliderGroup::addChild(): Child should be a slider!");
	CCNode::addChild(child, zOrder, tag);
	
	child->setAnchorPoint(ccp(0.5f, 0.5f));

	CCPoint pos(this->getContentSize().width/2.f, 0);

    CCObject* object = NULL;
	CCARRAY_FOREACH(this->getChildren(), object)
    {
		CCNode* node = (CCNode*)object;
        pos.y += node->getContentSize().height + CE_SLIDER_GROUP_SHIFT_Y;
	}
	pos.y -= CE_SLIDER_GROUP_SHIFT_Y;
	pos.y = (this->getContentSize().height - pos.y)/2.f;
	CCARRAY_FOREACH(this->getChildren(), object)
    {
		CCNode* node = (CCNode*)object;
		pos.y += node->getContentSize().height/2.f;
		node->setPosition(pos);
        pos.y += node->getContentSize().height/2.f + CE_SLIDER_GROUP_SHIFT_Y;
	}
}

}; // end namespace cocosext

